﻿using System;
using BikeBouncer.BusinessLayer;
using BikeBouncer.BusinessLayer.Entities;
using BikeBouncer.BusinessLayer.Enums;
using Devv.Core.Utils;

namespace BikeBouncer.Site
{
	public partial class BoxAddNote_aspx : BaseBoxPage
	{
		#region Page methods

		protected void Page_Load(object sender, EventArgs e)
		{
			if (Page.IsPostBack)
			{
				Page.Validate();

				if (Page.IsValid)
				{
					try
					{
						AddItemNote();
					}
					catch (Exception ex)
					{
						AppendError(ex);
					}
				}
				else
				{
					AppendError(GetGlobalString("PageNotValid"));
				}
			}
			else
			{
				ValTxtNoteDate.MinimumValue = DateTime.Now.AddYears(-1).ToShortDateString();
				ValTxtNoteDate.MaximumValue = DateTime.Now.ToShortDateString();

				AjaxManager.SetDefaults_AutoCompleteExtender(AutoComplete_TxtNoteCity);
				AjaxManager.SetDefaults_CalendarExtender(Calendar_TxtNoteDate);
				AjaxManager.SetDefaults_TextBoxWatermarkExtender(Watermark_TxtNoteLocation);
				
				LoadNote();
			}
		}

		#endregion

		#region Notes

		private void LoadNote()
		{
			PanNoteAdded.Visible = false;

			String refid = Request.QueryString["refid"];

			if (!DataUtil.IsNumeric(refid))
			{
				AppendError(GetLocalResourceObject("Error_InvalidRefid").ToString());
				PanForm.Visible = false;
				return;
			}

			Note note = new Note(Convert.ToInt32(refid));

			if (note.Id < 1)
			{
				AppendError(GetLocalResourceObject("Error_InvalidRefid").ToString());
				PanForm.Visible = false;
				return;
			}

			ViewState["ItemId"] = note.Item.Id;

			TxtNoteCity.Text = note.Address.City.ToString(", ");

			if (String.IsNullOrEmpty(TxtNoteCity.Text))
			{
				TxtNoteCity.Text = SessionManager.CurrentCity;
			}
			
			if (SessionManager.LoggedUser != null)
			{
				TxtNotePhone.Text = SessionManager.LoggedUser.Address.Phone;
			}
		}

		private void AddItemNote()
		{
			Note note = new Note();
			Item item = new Item(Convert.ToInt32(ViewState["ItemId"]));

			note.Item = item;
			note.Details = TxtNoteDetails.Text;
			note.Address.Postcode = TxtNotePostcode.Text;
			note.Address.Location = TxtNoteLocation.Text;
			note.Address.Phone = TxtNotePhone.Text;
			note.Address.Active = true;
			note.ReferenceDate = Convert.ToDateTime(TxtNoteDate.Text);
			note.IP = LocationManager.RequestIP;
			note.Active = true;

			note.Address.City = City.Fetch(TxtNoteCity.Text);

			if (String.IsNullOrEmpty(note.Address.City.Country))
			{
				note.Address.City.Country = SessionManager.CurrentCountry;
			}
			
			if (SessionManager.LoggedUser != null)
			{
				note.User = SessionManager.LoggedUser;
				note.Address.UserId = SessionManager.LoggedUser.Id;
			}

			if (note.Address.HasContents)
			{
				DataAccess.Address_Upsert(note.Address);
			}

			DataAccess.Note_Insert(note);

			UpdateItemStatus(item);

			PanForm.Visible = false;
			PanNoteAdded.Visible = true;
			
			if (SessionManager.LoggedUser == null)
			{
				PanOkRegister.Visible = true;
			}
			else
			{
				PanOkLogged.Visible = true;
				LitAddedLogged.Text = LitAddedLogged.Text.Replace("#EMAIL#", SessionManager.LoggedUser.Email);
			}
		}

		private void UpdateItemStatus(Item item)
		{
			item.Status = ItemStatus.Spotted;
			item.StatusDate = DateTime.Now;

			DataAccess.Item_Upsert(item);
		}

		#endregion
	}
}